# PinCaiCai 2.0 Raw SQL Application Package
from flask import Flask
import os

# Create Flask application
app = Flask(__name__, template_folder='templates', static_folder='static')

# Configuration
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', 'dev-secret-key-change-in-production')

# Import database utilities
from app.utils.database import get_db_connection, get_db_cursor

# Import logging middleware
from app.utils.middleware import log_api_requests

# Initialize logging middleware
log_api_requests(app)

# Import routes
from app.routes.auth import auth_bp
from app.routes.products import products_bp
from app.routes.cart import cart_bp
from app.routes.orders import orders_bp
from app.routes.review import review_bp

# Register blueprints
app.register_blueprint(auth_bp, url_prefix='/api')
app.register_blueprint(products_bp, url_prefix='/api')
app.register_blueprint(cart_bp, url_prefix='/api')
app.register_blueprint(orders_bp, url_prefix='/api')
app.register_blueprint(review_bp, url_prefix='/api')

# Home page route
@app.route('/')
def index():
    from flask import render_template
    return render_template('index.html')

@app.route('/login')
def login_page():
    from flask import render_template
    return render_template('login.html')

@app.route('/register')
def register_page():
    from flask import render_template
    return render_template('register.html')

@app.route('/vegetables')
def vegetables_page():
    from flask import render_template
    return render_template('vegetables.html')

@app.route('/meats')
def meats_page():
    from flask import render_template
    return render_template('meats.html')

@app.route('/fruits')
def fruits_page():
    from flask import render_template
    return render_template('fruits.html')

@app.route('/seafood')
def seafood_page():
    from flask import render_template
    return render_template('seafood.html')

@app.route('/all')
def all_products_page():
    from flask import render_template
    return render_template('all.html')

@app.route('/cart')
def cart_page():
    from flask import render_template
    return render_template('cart.html')

@app.route('/orders')
def orders_page():
    from flask import render_template
    return render_template('orders.html')

@app.route('/profile')
def profile_page():
    from flask import render_template
    return render_template('profile.html')

# Error handling
@app.errorhandler(404)
def not_found(error):
    from flask import render_template
    return render_template('404.html'), 404

@app.errorhandler(500)
def internal_error(error):
    from flask import render_template
    return render_template('500.html'), 500
